package com.mud.mudcommune.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mud.mudcommune.R;
import com.mud.mudcommune.view.widget.person_item;

import java.util.List;

/**
 * Created by welcome to china on 2016/12/10.
 */

public class MyAdapter extends BaseAdapter {
    private List<person_item> list;
    private Context context;

    public MyAdapter(List<person_item> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list==null?null:list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertview, ViewGroup viewGroup) {
        ViewHolder holder =null;
        person_item person_item = list.get(i);
        if(convertview==null){
           convertview= LayoutInflater.from(context).inflate(R.layout.item_text,null);}
             holder=getholder(convertview);
            String strings = person_item.getPinyin().charAt(0) + "";
            if(i!=0&&(strings.equals(list.get(i-1).getPinyin().charAt(0)+""))){
                holder.text.setVisibility(View.GONE);
            }else{
                holder.text.setVisibility(View.VISIBLE);
                holder.text.setText(strings);
            }
//        if(!(person_item.getName().charAt(0)=='长')){
            holder.name.setText(person_item.getName());//长字会归并到Z当中去(无解)
//        }
            return convertview;
    }
    private ViewHolder getholder(View convertView){
      ViewHolder viewHolder=null;
        viewHolder = (ViewHolder) convertView.getTag();
        if(viewHolder==null){
            viewHolder=new ViewHolder(convertView);
        }
        return viewHolder;
    }
    static class ViewHolder {
        private TextView name;
        private TextView text;
        public ViewHolder(View convertview) {
            text = (TextView) convertview.findViewById(R.id.text1);
            name = (TextView) convertview.findViewById(R.id.text2);
            convertview.setTag(this);
        }
    }
}
